The library() function loads “packages” that perfrom special functions into the workspace.
library(dplyr)
library(tigris)
library(tmap)
options(tigris_class = "sf") # Read shape files as Simiple Features objects
This step reads in a dataset from the cloud on votting patterns in Califonira. The dataset is from a California Open Data Set. The main portal home pages is here https://data.chhs.ca.gov/
voterData.t <- read.csv("https://data.chhs.ca.gov/dataset/40fd0792-2bfd-4303-a848-fc5cb4338295/resource/c384c86a-49d2-4128-8389-b2701ff0bc35/download/voter-registration-2002-2010.csv",as.is=TRUE)
This little step reads in a census-tract level geographic “shape file” for California from the US census. (A free “key” from the US Census in needed if you want to run it yourself; available here https://api.census.gov/data/key_signup.html. )
tracts_CA <- tracts(state = "CA", cb = TRUE)
voterData <- filter(voterData.t, geotype=="CT",
reportyear==2010,
race_eth_name=="Total",
type == "voted/registered") %>%
transform(GEOID=paste0("0",geotypevalue))
This step merges the data using “join”
map.1 <- left_join(tracts_CA, voterData, by="GEOID")
This step makes a very simple map, with coloring based on the percent in the census tract who voted
tmap_mode("view")
## tmap mode set to interactive viewing
tm_shape(map.1) + tm_polygons(col="percent")
## Linking to GEOS 3.6.1, GDAL 2.2.3, PROJ 4.9.3
This step filter the data to just Conta Costa County, and re-maps
map.2 <- filter(map.1,county_name == "Contra Costa")
tm_shape(map.2) + tm_polygons(col="percent")